package com.sl.au.controller;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.ContractBillInfo;
import com.sl.au.entity.Module;
import com.sl.au.entity.User;
import com.sl.au.service.PaymentSearchForViewService;
import com.sl.utils.ExdeUtil;

/**
 * 合同应收账查询控制器
 */
@RestController
@RequestMapping(value="/ui/payment-view")
public class PaymentSearchForViewController extends BaseController{
	@Autowired
	PaymentSearchForViewService paymentSearchForViewService;
	
	 @RequestMapping(method = RequestMethod.GET)
	    public ModelAndView index(){
	        ModelAndView modelAndView=createLayoutView("payment/paymentSearch");
	        return modelAndView;
	    }
	
	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> qMap = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		Map<String, Object>numMap=new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
//		Enumeration<String> paras = request.getParameterNames();
//		for(Enumeration e=paras;e.hasMoreElements();){
//		       String thisName=e.nextElement().toString();
//		       String thisValue=request.getParameter(thisName);
//		       if(thisName.equals("start") || thisName.equals("length") || thisName.equals("draw") || thisName.indexOf("[") != -1){
//		    	   continue;
//		       }
//		       System.out.println("thisName==>"+thisName+";thisValue===>"+thisValue);
//		       condition.put(thisName, thisValue);
//		}
		String monthYear = ExdeUtil.findYearMonth();
		List<ContractBillInfo> contractBillInfo = null;
		Account a = this.getMyAccount();
		User user = a.getUser();
		Module module = this.getPageModule("contractInfo");
		String dataFilter = module.getDataFilter();
		this.getQueryParams(qMap,numMap);
		System.out.println(numMap);
		condition.put("monthYear", monthYear);
		condition.put("dataFilter", dataFilter);
		condition.put("qMap", qMap);
		condition.put("numMap", numMap);
		//String superior = "销售处";
		//condition.put("superiorTop", "销售处");
		//condition.put("key", key);
		contractBillInfo = paymentSearchForViewService.getPayHistory(start, length,condition,user,null,null);
		long count =0;		
		count=paymentSearchForViewService.getPayHistory(0, Integer.MAX_VALUE,condition,user,null,null).size();
		map.put("data", contractBillInfo);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	private void getQueryParams(Map<String, Object> qMap,Map<String, Object>numMap){
		String names[]={"cOrderUnit","state","cProjectName",
				"contractType","cSupplierCode","cDemanderCode",
				"cProductKind","cCheckKind","cDutyOfficer",
				"cOrderArea","cPayType"};
		String nums[]={"nTotalSum","nWQPWDQYSSum","nYQPWDQYSSum","nWDQYSYFKSum","nWDQYSJDKSum","nWDQYSFHKSum",
				"nWDQYSYSKSum","nWDQYSTSKSum","nWDQYSZBKSum","nWDQYSYFSum","nNowMonthYSYFKSum","nNowMonthYSJDKSum",
				"nNowMonthYSFHKSum","nNowMonthYSYSKSum","nNowMonthYSTSKSum","nNowMonthYSZBKSum","nNowMonthYSYFSum",
				"nBeforeYSYFKSum","nBeforeYSJDKSum","nBeforeYSFHKSum","nBeforeYSYSKSum","nBeforeYSTSKSum","nBeforeYSZBKSum",
				"nBeforeYSYFSum","nNowMonthPayCheckSum","nEndingBalanceSum","nWQPContractEndingBalanceSum","nYSYHKSum",
				"nNowMonthGetPaySum","nWFHSKSum","nBeforeYearWDQYSKSum","nNowMonthContractAddSum","nTax","nBookBalanceSum"};
		String dates[]={"dSignedDate","dDeliveryDate","dLateestPayCheckDate"};
		for(String name:names){
			if(StringUtils.hasText(request.getParameter(name))){
				qMap.put(name, request.getParameter(name));
			}
		}
		for(String date:dates){
			if(null!=this.getDateQueryItem(date)){
				qMap.put(date,this.getDateQueryItem(date));
			}
		}
		for(String n:nums){
			if(null!=this.getNumQueryItem(n)){
				qMap.put(n, this.getNumQueryItem(n));
			}
		}
	}
}
